package club.kingyin.easycache.utils;


import club.kingyin.easycache.exception.SerializeException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class InstanceUtils {

    public static Object newInstance(Class type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Constructor declaredConstructor = type.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        return declaredConstructor.newInstance();
    }

    public static Object switchByType(String string,Class<?> type) {
        if (Integer.class.equals(type))
            return Integer.valueOf(string);
        if (String.class.equals(type))
            return string;
        if (Long.class.equals(type))
            return Long.valueOf(string);
        if (Double.class.equals(type))
            return Double.valueOf(string);
        if (Boolean.class.equals(type))
            return Boolean.valueOf(string);
        if (Short.class.equals(type))
            return Short.valueOf(string);
        if (Byte.class.equals(type))
            return Byte.valueOf(string);
        throw new SerializeException("暂不支持该类型转换 "+type);
    }

    public static Boolean isBaseType(Object o) {
        return o instanceof Integer || o instanceof String || o instanceof Short
                || o instanceof Long || o instanceof Double || o instanceof Boolean
                || o instanceof Byte || o instanceof Float;
    }

    public static Object switchByType(Object o,Class<?> type) {
        if (Integer.class.equals(type) || type.getName().equals("int")) {
            if (o instanceof String)
                return Integer.valueOf(((String) o));
            if(o instanceof Integer) {
                return o;
            }
        }
        if (String.class.equals(type))
            return String.valueOf(o);
        if (Long.class.equals(type) || type.getName().equals("long")) {
            if (o instanceof String)
                return Long.valueOf(((String) o));
            if(o instanceof Long) {
                return o;
            }
        }
        if (Double.class.equals(type) || type.getName().equals("double")) {
            if (o instanceof String)
                return Double.valueOf(((String) o));
            if(o instanceof Double) {
                return o;
            }
        }
        if (Boolean.class.equals(type) || type.getName().equals("boolean")) {
            if (o instanceof String)
                return Boolean.valueOf(((String) o));
            if(o instanceof Boolean) {
                return o;
            }
        }
        if (Short.class.equals(type) || type.getName().equals("short")) {
            if (o instanceof String)
                return Short.valueOf(((String) o));
            if(o instanceof Short) {
                return o;
            }
        }
        if (Byte.class.equals(type) || type.getName().equals("byte")) {
            if (o instanceof String)
                return Byte.valueOf(((String) o));
            if(o instanceof Byte) {
                return o;
            }
        }
        throw new SerializeException("暂不支持该类型转换 "+type);
    }
}
